/**
 * 图片资源管理器
 * 统一管理游戏中的所有图片资源
 */

export interface ImageResource {
  path: string
  width: number
  height: number
  description: string
}

export class ImageManager {
  private static instance: ImageManager
  private imageCache: Map<string, HTMLImageElement> = new Map()
  private imageResources: Map<string, ImageResource> = new Map()

  private constructor() {
    this.initializeImageResources()
  }

  public static getInstance(): ImageManager {
    if (!ImageManager.instance) {
      ImageManager.instance = new ImageManager()
    }
    return ImageManager.instance
  }

  /**
   * 初始化图片资源信息
   */
  private initializeImageResources(): void {
    // 角色立绘
    this.addImageResource('warrior', {
      path: '/assets/images/characters/warrior.png',
      width: 384,
      height: 512,
      description: '战士角色立绘',
    })

    this.addImageResource('mage', {
      path: '/assets/images/characters/mage.png',
      width: 384,
      height: 512,
      description: '法师角色立绘',
    })

    this.addImageResource('rogue', {
      path: '/assets/images/characters/rogue.png',
      width: 384,
      height: 512,
      description: '盗贼角色立绘',
    })

    this.addImageResource('priest', {
      path: '/assets/images/characters/priest.png',
      width: 384,
      height: 512,
      description: '牧师角色立绘',
    })

    // 武器图标
    this.addImageResource('sword', {
      path: '/assets/images/equipment/weapons/sword.png',
      width: 64,
      height: 64,
      description: '剑武器图标',
    })

    this.addImageResource('axe', {
      path: '/assets/images/equipment/weapons/axe.png',
      width: 64,
      height: 64,
      description: '斧武器图标',
    })

    this.addImageResource('staff', {
      path: '/assets/images/equipment/weapons/staff.png',
      width: 64,
      height: 64,
      description: '法杖武器图标',
    })

    this.addImageResource('dagger', {
      path: '/assets/images/equipment/weapons/dagger.png',
      width: 64,
      height: 64,
      description: '匕首武器图标',
    })

    // 防具图标
    this.addImageResource('cloth-armor', {
      path: '/assets/images/equipment/armor/cloth.png',
      width: 64,
      height: 64,
      description: '布甲防具图标',
    })

    this.addImageResource('leather-armor', {
      path: '/assets/images/equipment/armor/leather.png',
      width: 64,
      height: 64,
      description: '皮甲防具图标',
    })

    this.addImageResource('plate-armor', {
      path: '/assets/images/equipment/armor/plate.png',
      width: 64,
      height: 64,
      description: '板甲防具图标',
    })

    this.addImageResource('magic-robe', {
      path: '/assets/images/equipment/armor/robe.png',
      width: 64,
      height: 64,
      description: '魔法袍防具图标',
    })

    // 敌人形象
    this.addImageResource('skeleton', {
      path: '/assets/images/enemies/skeleton.png',
      width: 128,
      height: 128,
      description: '骷髅敌人',
    })

    this.addImageResource('goblin', {
      path: '/assets/images/enemies/goblin.png',
      width: 128,
      height: 128,
      description: '哥布林敌人',
    })

    this.addImageResource('orc', {
      path: '/assets/images/enemies/orc.png',
      width: 128,
      height: 128,
      description: '兽人敌人',
    })

    this.addImageResource('dark-mage', {
      path: '/assets/images/enemies/dark-mage.png',
      width: 128,
      height: 128,
      description: '暗黑法师敌人',
    })

    // UI元素
    this.addImageResource('start-button', {
      path: '/assets/images/ui/buttons/start.png',
      width: 200,
      height: 60,
      description: '开始游戏按钮',
    })

    this.addImageResource('settings-button', {
      path: '/assets/images/ui/buttons/settings.png',
      width: 200,
      height: 60,
      description: '设置按钮',
    })

    this.addImageResource('back-button', {
      path: '/assets/images/ui/buttons/back.png',
      width: 200,
      height: 60,
      description: '返回按钮',
    })

    // 图标
    this.addImageResource('health-icon', {
      path: '/assets/images/ui/icons/health.png',
      width: 32,
      height: 32,
      description: '生命值图标',
    })

    this.addImageResource('mana-icon', {
      path: '/assets/images/ui/icons/mana.png',
      width: 32,
      height: 32,
      description: '魔法值图标',
    })

    this.addImageResource('gold-icon', {
      path: '/assets/images/ui/icons/gold.png',
      width: 32,
      height: 32,
      description: '金币图标',
    })

    // 背景
    this.addImageResource('menu-background', {
      path: '/assets/images/ui/backgrounds/menu-bg.png',
      width: 1920,
      height: 1080,
      description: '主菜单背景',
    })

    this.addImageResource('game-background', {
      path: '/assets/images/ui/backgrounds/game-bg.png',
      width: 1920,
      height: 1080,
      description: '游戏背景',
    })

    // 地图瓦片
    this.addImageResource('floor-tile', {
      path: '/assets/images/tiles/floor.png',
      width: 32,
      height: 32,
      description: '地板瓦片',
    })

    this.addImageResource('wall-tile', {
      path: '/assets/images/tiles/wall.png',
      width: 32,
      height: 32,
      description: '墙壁瓦片',
    })

    this.addImageResource('door-tile', {
      path: '/assets/images/tiles/door.png',
      width: 32,
      height: 32,
      description: '门瓦片',
    })

    // 特效
    this.addImageResource('magic-effect', {
      path: '/assets/images/effects/magic.png',
      width: 64,
      height: 64,
      description: '魔法特效',
    })

    this.addImageResource('explosion-effect', {
      path: '/assets/images/effects/explosion.png',
      width: 64,
      height: 64,
      description: '爆炸特效',
    })

    this.addImageResource('heal-effect', {
      path: '/assets/images/effects/heal.png',
      width: 64,
      height: 64,
      description: '治疗效果',
    })
  }

  /**
   * 添加图片资源信息
   */
  private addImageResource(key: string, resource: ImageResource): void {
    this.imageResources.set(key, resource)
  }

  /**
   * 获取图片资源信息
   */
  public getImageResource(key: string): ImageResource | undefined {
    return this.imageResources.get(key)
  }

  /**
   * 加载图片
   */
  public async loadImage(key: string): Promise<HTMLImageElement> {
    // 检查缓存
    if (this.imageCache.has(key)) {
      return this.imageCache.get(key)!
    }

    const resource = this.imageResources.get(key)
    if (!resource) {
      throw new Error(`图片资源不存在: ${key}`)
    }

    return new Promise((resolve, reject) => {
      const img = new Image()
      img.onload = () => {
        this.imageCache.set(key, img)
        resolve(img)
      }
      img.onerror = () => {
        reject(new Error(`加载图片失败: ${resource.path}`))
      }
      img.src = resource.path
    })
  }

  /**
   * 预加载所有图片
   */
  public async preloadAllImages(): Promise<void> {
    const loadPromises: Promise<void>[] = []

    for (const key of this.imageResources.keys()) {
      loadPromises.push(
        this.loadImage(key)
          .then(() => {
            console.log(`图片加载成功: ${key}`)
          })
          .catch((error) => {
            console.warn(`图片加载失败: ${key}`, error)
          }),
      )
    }

    await Promise.allSettled(loadPromises)
    console.log('所有图片预加载完成')
  }

  /**
   * 获取所有图片资源键
   */
  public getAllImageKeys(): string[] {
    return Array.from(this.imageResources.keys())
  }

  /**
   * 清除图片缓存
   */
  public clearCache(): void {
    this.imageCache.clear()
  }
}

// 导出单例实例
export const imageManager = ImageManager.getInstance()
